package com.ruoyi.community.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.CommunityScope;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.mapper.AdPositionCommunityMapper;
import com.ruoyi.community.mapper.IntSetMapper;
import com.ruoyi.community.mapper.integral.IntUserStatisticMapper;
import com.ruoyi.system.domain.SysUserDept;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserDeptMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.community.mapper.SysCommunityMapper;
import com.ruoyi.community.domain.SysCommunity;
import com.ruoyi.community.service.ISysCommunityService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 社区Service业务层处理
 * 
 * @author 韩宁
 * @date 2021-08-01
 */
@Service
public class SysCommunityServiceImpl implements ISysCommunityService 
{
    @Autowired
    private SysCommunityMapper sysCommunityMapper;

    @Autowired
    private SysRoleMapper roleMapper;
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private SysUserDeptMapper userDeptMapper;

    @Autowired
    private IntSetMapper intSetMapper;

    @Autowired
    private IntUserStatisticMapper intUserStatisticMapper;

    @Autowired
    private AdPositionCommunityMapper adPositionCommunityMapper;

    /**
     * 查询社区
     * 
     * @param id 社区ID
     * @return 社区
     */
    @Override
    public SysCommunity selectSysCommunityById(Integer id)
    {
        SysCommunity community = sysCommunityMapper.selectSysCommunityById(id);
        return community;

    }

    /**
     * 查询社区列表
     * 
     * @param sysCommunity 社区
     * @return 社区
     */
    @Override
    public List<SysCommunity> selectSysCommunityList(SysCommunity sysCommunity)
    {
        sysCommunity.setUserId(SecurityUtils.getUserID());
        return sysCommunityMapper.selectSysCommunityList(sysCommunity);
    }

    /**
     * 查询用户未加入的社区列表
     * @param userId
     * @return
     */
    @Override
    public List<SysCommunity> selectListOther(Long userId) {
        return sysCommunityMapper.selectListOther(userId);
    }

    /**
     * 新增社区
     * 
     * @param sysCommunity 社区
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysCommunity(SysCommunity sysCommunity)
    {

        Long userId = SecurityUtils.getUserID();
        sysCommunity.setCreateTime(DateUtils.getNowDate());
        sysCommunity.setUserId(userId);
        // 添加一个社区返回 communityId
        sysCommunityMapper.insertSysCommunity(sysCommunity);
        Integer communityId = sysCommunity.getCommunityId();
        // 添加一个默认的该社区admin角色
        SysRole role = initRole(sysCommunity);
        roleMapper.insertRole(role);
        // 该用户关联 该社区 admin 角色；
        SysUserRole userRole = new SysUserRole(userId,role.getRoleId());
        List<SysUserRole> list = new ArrayList<>();
        list.add(userRole);
        userRoleMapper.batchUserRole(list);
        // 默认生成该社区分组：社区名
        SysDept dept = initDept(sysCommunity);
        deptMapper.insertDept(dept);
        // 生成物业管理子部门
        SysDept childDept = initChildDept(dept);
        deptMapper.insertDept(childDept);
        // 将用户添加到 物业管理 部门中
        SysUserDept userDept = new SysUserDept(userId, childDept.getDeptId(), SecurityUtils.getCommunityId());
        userDeptMapper.insertSysUserDept(userDept);
        // 创建社区后初始化积分设置 int_set
        intSetMapper.initSetByCommunity(communityId);
        // 初始化广告位置
        adPositionCommunityMapper.init(communityId);
        // 用户加入到社区，初始化积分设置，int_user_statistic
        return intUserStatisticMapper.insertIntUserStatistic(userId, communityId);
    }

    /**
     * 初始化 社区 分组
     */
    public SysDept initDept(SysCommunity community) {
        SysDept dept = new SysDept();
        dept.setDeptName(community.getName());
        dept.setParentId(0L);
        dept.setType("0");
        dept.setOrderNum("1");
        dept.setCreateBy(SecurityUtils.getUsername());
        dept.setCommunityId(community.getCommunityId());
        dept.setAncestors("0");
        dept.setCreateTime(new Date());
        return dept;
    }

    /**
     * 生成物业管理子部门
     */
    public  SysDept initChildDept(SysDept parent) {
        SysDept dept = new SysDept();
        dept.setDeptName("物业管理");
        dept.setParentId(parent.getDeptId());
        dept.setType("1");
        dept.setOrderNum("1");
        dept.setCreateBy(SecurityUtils.getUsername());
        dept.setCommunityId(parent.getCommunityId());
        dept.setAncestors(parent.getAncestors() + "," + parent.getDeptId());
        dept.setCreateTime(new Date());
        return dept;
    }
    /** 初始化社区 admin 角色 **/
    public SysRole initRole(SysCommunity community) {
        SysRole role = new SysRole();
        role.setRoleName(community.getCommunityId()+"_管理员");
        role.setRoleKey("admin");
        role.setRoleSort("1");
        role.setStatus("0");
        role.setCreateBy(SecurityUtils.getUsername());
        role.setCommunityId(community.getCommunityId());
        return role;
    }
    /**
     * 修改社区
     * 
     * @param sysCommunity 社区
     * @return 结果
     */
    @Override
    public int updateSysCommunity(SysCommunity sysCommunity)
    {
        return sysCommunityMapper.updateSysCommunity(sysCommunity);
    }

    /**
     * 批量删除社区
     * 
     * @param ids 需要删除的社区ID
     * @return 结果
     */
    @Override
    public int deleteSysCommunityByIds(Long[] ids)
    {
        return sysCommunityMapper.deleteSysCommunityByIds(ids);
    }

    /**
     * 删除社区信息
     * 
     * @param id 社区ID
     * @return 结果
     */
    @Override
    public int deleteSysCommunityById(Long id)
    {
        return sysCommunityMapper.deleteSysCommunityById(id);
    }

    @Override
    public List<SysCommunity> getAllCommunity() {
        return sysCommunityMapper.getAllCommunity();
    }
}
